<template>
	<view :data-theme="theme">
		<view class="ChangePassword">
			<view class="list">
				<block >
					<view class="item">
						<input disabled='true' :value="userInfo.phone"></input>
					</view>
					<view class="item">
						<input type='number' password  placeholder='新支付密码' placeholder-class='placeholder' v-model="payPwd"
							maxlength="6"></input>
					</view>
					<view class="item">
						<input type='number' password  placeholder='确认新支付密码' placeholder-class='placeholder' v-model="payPwd2"
							maxlength="6"></input>
					</view>
					<view class="item acea-row row-between-wrapper">
						<input type='number' placeholder='填写验证码' placeholder-class='placeholder' class="codeIput"
							v-model="captcha" maxlength="6"></input>
						<button class="code" :class="disabled === true ? 'on' : ''" :disabled='disabled' @click="code">
							{{ text }}
						</button>
					</view>
				</block>
			</view>
			<button form-type="submit" class="confirmBnt bg_color" @click="editPwd">保存</button>
		</view>
	</view>
</template>

<script>
	// +----------------------------------------------------------------------
	// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
	// +----------------------------------------------------------------------
	// | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
	// +----------------------------------------------------------------------
	// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
	// +----------------------------------------------------------------------
	// | Author: CRMEB Team <admin@crmeb.com>
	// +----------------------------------------------------------------------
	import sendVerifyCode from "@/mixins/SendVerifyCode";
	import {
		updatePayPwdCode,
		payPwdReset,
		verifyCode,
		bindingVerify
	} from '@/api/api.js';
	import {
		toLogin
	} from '@/libs/login.js';
	import {
		mapGetters
	} from "vuex";
	import {
		setThemeColor
	} from '@/utils/setTheme.js'
	import {
		Debounce
	} from '@/utils/validate.js'
	import Verify from '../components/verifition/verify.vue';
	const app = getApp();
	export default {
		components: {
			Verify
		},
		mixins: [sendVerifyCode],
		data() {
			return {
				payPwd: '',
				payPwd2: '',
				captcha: '',
				isAuto: false, //没有授权的不会自动授权
				isShowAuth: false, //是否隐藏授权
				timer: '',
				text: '获取验证码',
				nums: 60,
				theme: app.globalData.theme,
				bgColor: '',
			};
		},
		mounted() {
			// this.timer = setInterval(this.getTimes, 1000);
		},
		computed: mapGetters(['isLogin', 'userInfo']),
		onLoad() {
			let that = this;
			that.bgColor = setThemeColor();
			uni.setNavigationBarColor({
				frontColor: '#ffffff',
				backgroundColor: that.bgColor,
			});
			if (this.isLogin) {
				// verifyCode().then(res=>{
				// 	this.$set(this, 'key', res.data.key)
				// });
			} else {
				toLogin();
			}
		},
		methods: {
			getTimes() {
				this.nums = this.nums - 1;
				this.text = "剩余 " + this.nums + "s";
				if (this.nums < 0) {
					clearInterval(this.timer);
				}
				this.text = "剩余 " + this.nums + "s";
				if (this.text < "剩余 " + 0 + "s") {
					this.disabled = false;
					this.text = "重新获取";
				}
			},
			onLoadFun: function() {},
			// 授权关闭
			authColse: function(e) {
				this.isShowAuth = e
			},
			editPwd: Debounce(function(e) {
				let that = this;
				if (!that.payPwd) return that.$util.Tips({
					title: '请填写支付密码！'
				});
				if (!(/^[0-9]{6}$/i.test(that.payPwd))) return that.$util.Tips({
					title: '请输入正确的支付密码！'
				});
				if (!that.payPwd2) return that.$util.Tips({
					title: '请确认支付密码！'
				});
				if (!(that.payPwd===that.payPwd2)) return that.$util.Tips({
					title: '两次输入支付密码不一致！'
				});
				if (!that.captcha) return that.$util.Tips({
					title: '请填写验证码'
				});
				uni.showModal({
					title: '是否修改支付密码',
					confirmText: '修改',
					success(res) {
						uni.showLoading({
							title: '加载中',
							mask: true
						});
						if (res.confirm) {
							payPwdReset({
								password: that.payPwd,
								code: that.captcha
							}).then(res => {
								uni.hideLoading();
								that.$util.Tips({
									title: res.message
								});
								uni.navigateBack();
							}).catch(err => {
								uni.hideLoading();
								return that.$util.Tips({
									title: err
								});
							})
						}
					}
				});
			}),
			/**
			 * 发送验证码
			 *
			 */
			code: Debounce(function() {
				let that = this;
				if (!that.payPwd) return that.$util.Tips({
					title: '请填写支付密码！'
				});
				if (!(/^[0-9]{6}$/i.test(that.payPwd))) return that.$util.Tips({
					title: '请输入正确的支付密码！'
				});
				if (!that.payPwd2) return that.$util.Tips({
					title: '请确认支付密码！'
				});
				if (!(that.payPwd===that.payPwd2)) return that.$util.Tips({
					title: '两次输入支付密码不一致！'
				});
				
				that.nums = 60;
				uni.showLoading({
					title: '加载中',
					mask: true
				});
				updatePayPwdCode().then(res => {
					that.$util.Tips({
						title: res.message
					});
					that.timer = setInterval(that.getTimes, 1000);
					that.disabled = true;
					uni.hideLoading();
				}).catch(err => {
					return that.$util.Tips({
						title: err
					});
					uni.hideLoading();
				});

			})
		}
	}
</script>

<style lang="scss" scoped>
	.shading {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 100%;

		/* #ifdef APP-VUE */
		margin-top: 50rpx;
		/* #endif */
		/* #ifndef APP-VUE */

		margin-top: 200rpx;
		/* #endif */


		image {
			width: 180rpx;
			height: 180rpx;
		}
	}

	page {
		background-color: #fff !important;
	}

	.ChangePassword {
		padding-top: 100rpx;
	}

	.ChangePassword .phone {
		font-size: 32rpx;
		font-weight: bold;
		text-align: center;
		margin-top: 55rpx;
	}

	.ChangePassword .list {
		width: 580rpx;
		margin: 0 auto;
	}

	.ChangePassword .list .item {
		width: 100%;
		height: 110rpx;
		border-bottom: 2rpx solid #f0f0f0;
	}

	.ChangePassword .list .item input {
		width: 100%;
		height: 100%;
		font-size: 32rpx;
	}

	.ChangePassword .list .item .placeholder {
		color: #b9b9bc;
	}

	.ChangePassword .list .item input.codeIput {
		width: 340rpx;
	}

	.ChangePassword .list .item .code {
		font-size: 32rpx;
		@include main_color(theme);
		background-color: #f5f5f5;
	}

	.ChangePassword .list .item .code.on {
		color: #b9b9bc !important;
	}

	.ChangePassword .confirmBnt {
		font-size: 32rpx;
		width: 580rpx;
		height: 90rpx;
		border-radius: 45rpx;
		color: #fff;
		margin: 92rpx auto 0 auto;
		text-align: center;
		line-height: 90rpx;
	}

	.bg_color {
		@include main_bg_color(theme);
	}
</style>
